<template>
  <!-- 授权用户 -->
  <el-dialog title="选择角色" :visible.sync="visible" width="80%" top="5vh" append-to-body>
    <el-form ref="queryForm" :model="queryParams" size="small" label-width="100px" inline>
      <el-form-item label="角色名称:" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色名称"
          clearable
          style="width: 186px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="search-form-button">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="table" tooltip-effect="light" :data="roleList" border @row-click="clickRow" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="角色编号" prop="roleId" show-overflow-tooltip />
      <el-table-column label="角色名称" prop="roleName" show-overflow-tooltip />
      <el-table-column label="权限字符" prop="roleKey" show-overflow-tooltip />
      <el-table-column label="状态" align="center" width="70" prop="status">
        <template slot-scope="scope">
          <span class="stateShell">
            <div v-if="scope.row.status==='0'" class="common-prompt" style="background-color:#67C23A" />
            <div v-else class="common-prompt" style="background-color:#F56C6C" />
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
          </span></template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :pager-count="5"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="submitLoading" @click="handleSelectUser">确 定</el-button>
      <el-button @click="cacel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { insertAuthRoleAll, unallocatedRoleList } from '@/api/system/menu'

export default {
  dicts: ['sys_normal_disable'],
  props: {
    // 菜单编号
    menuId: {
      type: [Number, String]
    },
    appId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      ids: [],
      // 遮罩层
      visible: false,
      submitLoading: false,
      // 选中数组值
      roleIds: [],
      // 总条数
      total: 0,
      // 未授权用户数据
      roleList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menuId: 0,
        appId: 0,
        roleName: undefined
      }
    }
  },
  methods: {
    // 显示弹框
    show(ids) {
      this.ids = ids
      this.queryParams.roleName = null
      this.submitLoading = false
      this.queryParams.menuId = this.menuId
      this.queryParams.appId = this.appId
      this.getList()
      this.visible = true
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.roleIds = selection.map(item => item.roleId)
    },
    // 查询表数据
    getList() {
      unallocatedRoleList(this.queryParams).then(res => {
        this.roleList = res.rows
        this.total = res.total
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 取消按钮*/
    cacel() {
      this.queryParams.nickName = null
      this.queryParams.phonenumber = null
      this.getList()
      this.visible = false
    },
    /** 选择授权用户操作 */
    handleSelectUser() {
      this.submitLoading = true
      const roleIds = this.roleIds.join(',')
      const ids = this.ids.join(',')

      if (roleIds === '') {
        this.$modal.msgError('请选择要分配的角色')
        this.submitLoading = false
        return
      }
      insertAuthRoleAll({ menuIds: ids, roleIds: roleIds }).then(res => {
        this.$modal.msgSuccess(res.msg)
        if (res.code === 200) {
          this.visible = false
          this.$emit('ok')
        }
      }).catch(err => {
        this.submitLoading = false
        console.error(err)
      })
    }
  }
}
</script>
